<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/admin.css">
    <link rel="stylesheet" href="/static/md/css/editormd.css"/>
    <title>许久没道过早安的后台</title>
    <style>
        #input{
            width: 500px;
            margin-top:20px;
        }
        #label{
            width:800px;
        }
        #submit{
            /*float:right;*/
            margin-left:50px;
        }
    </style>
</head>
<body>
<div class="body">
    <div class="layui-fluid">
        {include file='public/sider' /}
        <div id="main">
            {include file='public/header' /}

            <div class="layui-fluid" id="body">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <div class="layui-form" lay-filter="article_edit">
                            <div id="input">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">
                                        文章标题
                                    </label>
                                    <div class="layui-input-block">
                                        <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">
                                        文章类别
                                    </label>
                                    <div class="layui-input-block" id="label">
                                        {volist name="labels" id="vo"}
                                        {if in_array($vo.id,$article['label'])}
                                        <input type="checkbox" name="label" value="{$vo.id}" title="{$vo.label}" checked>
                                        {else}
                                        <input type="checkbox" name="label" value="{$vo.id}" title="{$vo.label}">
                                        {/if}
                                        {/volist}
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    文章内容
                                </label>
                                <div id="md">
                                    <textarea id="my-md-md" name="content" style="display:none;">
                                        {$article['md_content']}
                                    </textarea>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">
                                    是否发布
                                </label>
                                <div class="layui-input-block">
                                    {if $article['status']==1}
                                    <input type="checkbox" name="status" value="1" title="发布" checked>
                                    {else}
                                    <input type="checkbox" name="status" value="1" title="发布">
                                    {/if}
                                </div>
                            </div>
                            <div id="submit">
                                <div class="layui-form-item">
                                    <button class="layui-btn" lay-submit lay-filter="form">提交</button>
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<script src="/static/jquery.min.js"></script>
<script src="/static/layui/layui.js"></script>
<script src="/static/md/editormd.min.js"></script>
<script>
    var height = window.screen.availHeight;
    document.getElementById('body').style.height = parseInt(height) - 190 + "px";
    document.getElementById('sider').style.height = parseInt(height) - 165 + "px";
    layui.use(['jquery','form','layer','layedit'],function(){
        var $ = layui.jquery;
        var form = layui.form;
        var layedit = layui.layedit;
        var layer = layui.layer;
        // 表单初始值
        form.val("article_edit", {
            "title": "{$article['title']}" // "name": "value"

        });
        // 采集复选框的值
        var checkbox = [];
        form.on('checkbox()',function(data){
            var index = checkbox.indexOf(data.value);
            if (index != -1) checkbox.splice(index,1);
            else checkbox.push(data.value);
        });
        $(function () {
            editormd("md", {
                width: "90%",
                height: 500,
                syncScrolling: "single",
                path: "/static/md/lib/",//注意2：你的路径
                saveHTMLToTextarea: true,//注意3：这个配置，方便post提交表单
                emoji: true,

                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: "{:url('common/upload')}",//注意你后端的上传图片服务地址

            });
        });


        // 监听表单提交
        form.on('submit(form)',function(data){
            data.field['id'] = "{$article['id']}";
            data.field['label'] = checkbox;
            // layer.msg(JSON.stringify(data.field));
            $.ajax({
                async: true,
                type: "POST",
                url: "{:url('article/edit_action')}",
                data: data.field,
                success:function(data){
                    if(data.status == 200){
                        layer.msg(data.msg);
                        setTimeout(function(){
                            window.location.href="{:url('article/lst')}";
                        }, 2000);
                    }else{
                        layer.msg(data.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>